﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;


namespace doveData
{
    public static class StatContrl
    {
        public static StationGraph<Station> AllStation = new StationGraph<Station>(); //存储站点的图
        public static List<Hotel> AllHotel = new List<Hotel>();
        public static List<Food> AllFood = new List<Food>();
        public static List<Market> AllMarket = new List<Market>();
        public static Dictionary<string, List<Station>> LineDict = new Dictionary<string, List<Station>>(); //路线字典

        public static Dictionary<string, int> NameDict = new Dictionary<string, int>(); //Key储存已有名字的集合,Value为对应在图中的索引
        public static List<string> NearNameList=new List<string>();
        //初始化以邻接矩阵存储的图结构
        public static void InitStationM(XDocument xml)
        {
            //id 线路
            //st 站点
            int index = 0;
            string tmpline = "";
            var query = from q in xml.Descendants("st")
                        select q;
            foreach (XElement Element in query)
            {
                string line = Element.Parent.Attributes().ElementAt(0).Value;
                string name = Element.Value;
                float locX, locY;
                locX = float.Parse(Element.Attributes().ElementAt(1).Value);
                locY = float.Parse(Element.Attributes().ElementAt(2).Value);
                Vector2 location = new Vector2(locX, locY);
                Station tmpStation;
                if (tmpline != line)
                {
                    tmpline = line;
                    index = 0;
                }
                if (LineDict.ContainsKey(line))
                {
                }
                else
                {
                    LineDict.Add(line, new List<Station>());
                }
                if (NameDict.ContainsKey(name))
                {
                    tmpStation = AllStation.GetItem(NameDict[name]);
                    LineDict[line].Add(tmpStation);
                    tmpStation.LineDict.Add(line, index.ToString());
                }
                else
                {
                    string id = line + LineDict[line].Count.ToString();
                    tmpStation = new Station(id, line, index.ToString(), name, location);
                    AllStation.AddVertex(tmpStation);
                    LineDict[line].Add(tmpStation);
                    NameDict.Add(name, AllStation.Count - 1);
                }
                index += 1;
            }
            AllStation.InitMatrix();//Add点超过默认的容量后新建的邻接矩阵需要重新初始化
            foreach (string lineDictKey in LineDict.Keys)
            {
                var currentLine = LineDict[lineDictKey];
                for (int i = 0; i < currentLine.Count; i++)
                {
                    if (i != 0)
                    {
                        float distance = (float)Vector2.Distance(currentLine[i].Location, currentLine[i - 1].Location);
                        AllStation.AddEdgeM(currentLine[i], currentLine[i - 1], distance);
                    }
                }
            }
        }

        public static void ShowByLine()
        {
            foreach (string lineDictKey in LineDict.Keys)
            {
                var currentLine = LineDict[lineDictKey];
                for (int i = 0; i < currentLine.Count; i++)
                {
                    Console.WriteLine(lineDictKey + "号线" + currentLine[i]);
                }

            }
        }

        public static void CheckChangeStation()
        {
            List<string> names = new List<string>();
            Console.WriteLine("换乘站有：");
            foreach (string lineDictKey in LineDict.Keys)
            {
                var currentLine = LineDict[lineDictKey];
                for (int i = 0; i < currentLine.Count; i++)
                {
                    if (currentLine[i].LineDict.Count > 1 && names.Contains(currentLine[i].Name) == false)
                    {
                        Console.WriteLine(currentLine[i].ToString());
                        names.Add(currentLine[i].Name);
                    }
                }
            }
        }

        public static List<Station> GetLeastChange(string from, string to)
        {
            List<Station> output=new List<Station>(10);
            if (AllStation.Count == 0)
            {
                Console.WriteLine("尚未初始化线路！");
                return output;
            }
            if (!NameDict.ContainsKey(from) || !NameDict.ContainsKey(to))
            {
                Console.WriteLine("输入错误！");
                return output;
            }
            Station fromS, toS;
            fromS = AllStation.GetItem(NameDict[from]);
            toS = AllStation.GetItem(NameDict[to]);
            Queue<Station> q = new Queue<Station>();
            Stack<Station> message = new Stack<Station>();
            List<String> hasTravel = new List<string>();
            W_LineTraveres(fromS, fromS, toS, q, message, hasTravel);
            
            int mesCount = message.Count;
            for (int i = 0; i < mesCount; i++)
            {
                output.Add(message.Pop());
            }
            return output;
        }
        public static void LeastChange(string from, string to)
        {
            if (AllStation.Count == 0)
            {
                Console.WriteLine("尚未初始化线路！");
                return;
            }
            if (!NameDict.ContainsKey(from) || !NameDict.ContainsKey(to))
            {
                Console.WriteLine("输入错误！");
                return;
            }
            Station fromS, toS;
            fromS = AllStation.GetItem(NameDict[from]);
            toS = AllStation.GetItem(NameDict[to]);
            Queue<Station> q = new Queue<Station>();
            Stack<String> message = new Stack<string>();
            List<String> hasTravel = new List<string>();
            LineTraveres(fromS,fromS, toS, q, message, hasTravel);
            Console.WriteLine("从{0}坐地铁到{1}的最少换乘坐法是：", from, to);
            int mesCount = message.Count;
            for (int i = 1; i <= mesCount; i++)
            {
                Console.Write(message.Pop());
            }
        }

        private static int W_LineTraveres(Station source, Station s, Station destination, Queue<Station> q, Stack<Station> st, List<String> hasTravel)
        {//循环
            //Console.WriteLine("执行{0}循环",s.Name);
            foreach (string line in s.LineDict.Keys)//遍历所有路线
            {
                if (hasTravel.Contains(line) == false)
                {
                    foreach (Station sInLine in LineDict[line])//遍历路线的所有站
                    {
                        if (sInLine == destination)//1、找到线路中的目的地
                        {
                            //Console.WriteLine("!test!找到了!{0}的{1}线", s.Name, line);
                            st.Push(destination);
                            st.Push(s);
                            //Console.WriteLine("跳出{0}循环-1",s.Name);
                            return 1;
                        }
                    }
                    hasTravel.Add(line);
                    //2、找不到目的地
                    //所有站的所有路线
                    List<Station> lineList = LineDict[line];
                    int stIndex = lineList.IndexOf(s);
                    Station testStation = s;
                    for (int i = 1; i < lineList.Count - 1; i++)
                    {
                        int searchIndex = stIndex + i;
                        if (searchIndex >= 0 && searchIndex < lineList.Count)
                        {
                            testStation = lineList[searchIndex];
                            if (testStation.LineDict.Count != 1 && testStation != s)
                            {
                                if (!q.Contains(testStation))
                                {
                                    //Console.WriteLine("!test!:将{1}{0}入队", testStation.Name,line);
                                    q.Enqueue(testStation);
                                }
                            }
                        }
                        searchIndex = stIndex - i;

                        if (searchIndex >= 0 && searchIndex < lineList.Count)
                        {
                            testStation = lineList[searchIndex];
                            if (testStation.LineDict.Count != 1 && testStation != s)
                            {
                                if (!q.Contains(testStation))
                                {
                                    //Console.WriteLine("!test!:将{1}{0}入队", testStation.Name,line);
                                    q.Enqueue(testStation);
                                }
                            }
                        }
                    }
                }
            }
            //处理队列
            if (q.Count != 0)
            {
                Station stQ = q.Dequeue();
                //Console.WriteLine("!test!{0}出队!", stQ.Name);
                int getcha = W_LineTraveres(source, stQ, destination, q, st, hasTravel);
                if (getcha == 1)
                {
                    foreach (string currentline in s.LineDict.Keys)
                    {
                        foreach (string stline in stQ.LineDict.Keys)
                        {
                            if (currentline == stline)
                            {
                                if (source.LineDict.ContainsKey(stline))
                                {
                                    s = source;
                                    st.Push(s);
                                    return -2;
                                }
                                st.Push(s);
                            }
                        }
                    }
                    //Console.WriteLine("跳出{0}循环-1", s.Name);
                    return 1;
                }
            }
            //Console.WriteLine("跳出{0}循环-2", s.Name);
            return -2;
        }
        private static int LineTraveres(Station source,Station s, Station destination, Queue<Station> q, Stack<string> st, List<String> hasTravel)
        {//循环
            //Console.WriteLine("执行{0}循环",s.Name);
            foreach (string line in s.LineDict.Keys)//遍历所有路线
            {
                if (hasTravel.Contains(line) == false)
                {
                    foreach (Station sInLine in LineDict[line])//遍历路线的所有站
                    {
                        if (sInLine == destination)//1、找到线路中的目的地
                        {
                            //Console.WriteLine("!test!找到了!{0}的{1}线", s.Name, line);
                            st.Push("乘坐" + line + "号线:" + s.Name + "——>" + destination.Name + "\n");
                            //Console.WriteLine("跳出{0}循环-1",s.Name);
                            return 1;
                        }
                    }
                    hasTravel.Add(line);
                    //2、找不到目的地
                    //所有站的所有路线
                    List<Station> lineList=LineDict[line];
                    int stIndex = lineList.IndexOf(s);
                    Station testStation=s;
                    for (int i = 1; i < lineList.Count - 1; i++)
                    {
                        int searchIndex = stIndex + i;
                        if (searchIndex >= 0 && searchIndex < lineList.Count)
                        {
                            testStation = lineList[searchIndex];
                            if (testStation.LineDict.Count != 1 && testStation != s)
                            {
                                if (!q.Contains(testStation))
                                {
                                    //Console.WriteLine("!test!:将{1}{0}入队", testStation.Name,line);
                                    q.Enqueue(testStation);
                                }
                            }
                        }
                        searchIndex = stIndex - i;
                        
                        if (searchIndex >= 0 && searchIndex < lineList.Count)
                        {
                            testStation = lineList[searchIndex];
                            if (testStation.LineDict.Count != 1 && testStation != s)
                            {
                                if (!q.Contains(testStation))
                                {
                                    //Console.WriteLine("!test!:将{1}{0}入队", testStation.Name,line);
                                    q.Enqueue(testStation);
                                }
                            }
                        }
                    }
                }
            }
            //处理队列
            if (q.Count != 0)
            {
                Station stQ = q.Dequeue();
                //Console.WriteLine("!test!{0}出队!", stQ.Name);
                int getcha = LineTraveres(source,stQ, destination, q, st, hasTravel);
                if (getcha == 1)
                {
                    foreach (string currentline in s.LineDict.Keys)
                    {
                        foreach (string stline in stQ.LineDict.Keys)
                        {
                            if (currentline == stline)
                            {
                                if (source.LineDict.ContainsKey(stline))
                                {
                                    s = source;
                                    st.Push("!乘坐" + stline + "号线:" + s.Name + "——>" + stQ.Name + "\n");
                                    return -2;
                                }
                                st.Push("!乘坐" + stline + "号线:" + s.Name + "——>" + stQ.Name + "\n");
                            }
                        }
                    }
                    //Console.WriteLine("跳出{0}循环-1", s.Name);
                    return 1;
                }
            }
            //Console.WriteLine("跳出{0}循环-2", s.Name);
            return -2;
        }


        public static void ShortestPath(string from, string to)
        {
            if (AllStation.Count == 0)
            {
                Console.WriteLine("尚未初始化线路！");
                return;
            }
            Console.WriteLine("正尝试查询{0}到{1}的最短路径...", from, to);
            if (NameDict.ContainsKey(from) && NameDict.ContainsKey(to))
            {
                AllStation.ShortestPath_DIJ(NameDict[from], NameDict[to]);
            }
            else
            {
                Console.WriteLine("查询的站点不存在！");
            }

        }



        public static void LeastTime(string from, string to)
        {
            if (AllStation.Count == 0)
            {
                Console.WriteLine("尚未初始化线路！");
                return;
            }
            float distance = 0;
            if (NameDict.ContainsKey(from) && NameDict.ContainsKey(to))
            {
                distance = AllStation.GetShortestPath_DIJ(NameDict[from], NameDict[to]);
            }
            else
            {
                Console.WriteLine("查询的站点不存在！");
                return;
            }
            float velocity = 80;
            float Time = 0;
            Time = 2 * distance / velocity * 60;
            Console.WriteLine("预计最短用时：" + Time + "分钟");
        }

        public static void LeastMoney(string from, string to)
        {
            if (AllStation.Count == 0)
            {
                Console.WriteLine("尚未初始化线路！");
                return;
            }
            float distance = 0;
            if (NameDict.ContainsKey(from) && NameDict.ContainsKey(to))
            {
                distance = AllStation.GetShortestPath_DIJ(NameDict[from], NameDict[to]);
            }
            else
            {
                Console.WriteLine("查询的站点不存在！");
                return;
            }
            float money = 0;
            //Distance每两个单位为一公里
            if (distance < 8)
            {
                money = 2;
            }
            else if (distance >= 8 && distance < 24)
            {
                money = (float)Math.Floor((distance - 8) / 8);
                money += 2;
            }
            else if (distance >= 24 && distance < 48)
            {
                money = (float)Math.Floor((distance - 24) / 12);
                money += 4;
            }
            else
            {
                money = (float)Math.Floor((distance - 48) / 16);
                money += 6;
            }
            Console.WriteLine("预计花费：{0}元", money);
        }

        public static void InitNear(XDocument xml)
        {
            //1、获取XML类型
            //2、通过类型读取构造函数参数列表
            //3、对比参数列表与xml下的结点进行赋值
            string className = xml.Elements().First().Name.ToString();

            if (className != "Food" && className != "Market" && className != "Hotel")
            {
                return;
            }

            var query = from q in xml.Descendants(className.ToLower())
                        select q;
            if (className == "Food")
            {
                foreach (XElement element in query)
                {
                    string id = element.Attributes().First().Value;
                    string name = element.Element("name").Value;
                    float score = float.Parse(element.Element("score").Value);
                    float price = float.Parse(element.Element("price").Value);
                    Vector2 location = new Vector2(int.Parse(element.Element("x").Value), int.Parse(element.Element("y").Value));
                    AllFood.Add(new Food(id, score, price, location, name));
                    NearNameList.Add(name);
                }
            }
            else if (className == "Market")
            {
                foreach (XElement element in query)
                {
                    string id = element.Attributes().First().Value;
                    string name = element.Element("name").Value;
                    float score = float.Parse(element.Element("score").Value);
                    string goods = element.Element("goods").Value;
                    Vector2 location = new Vector2(int.Parse(element.Element("x").Value), int.Parse(element.Element("y").Value));
                    AllMarket.Add(new Market(id, score, goods, location, name));
                    NearNameList.Add(name);
                }
            }
            else if (className == "Hotel")
            {
                foreach (XElement element in query)
                {
                    string id = element.Attributes().First().Value;
                    string name = element.Element("name").Value;
                    float score = float.Parse(element.Element("score").Value);
                    float price = float.Parse(element.Element("price").Value);
                    float stars = float.Parse(element.Element("stars").Value);
                    Vector2 location = new Vector2(int.Parse(element.Element("x").Value), int.Parse(element.Element("y").Value));
                    AllHotel.Add(new Hotel(id, stars, score, price, location, name));
                    NearNameList.Add(name);
                }
            }

        }

        public static void CheckStation(string stationName, float distance)
        {
            if (AllStation.Count == 0)
            {
                Console.WriteLine("尚未初始化线路！");
                return;
            }
            if (!NameDict.ContainsKey(stationName))
            {
                Console.WriteLine("没有这个站");
                return;
            }
            float tempDist = 0;
            Vector2 pos = AllStation.GetItem(NameDict[stationName]).Location;
            Console.WriteLine("  {0}站附近有：", stationName);
            Console.WriteLine("=====饭店=====");
            foreach (Food food in AllFood)
            {
                tempDist = (float)Vector2.Distance(food.Location, pos);
                if (tempDist < distance)
                {
                    Console.WriteLine("{0}，距离{1}km", food.Name, Math.Round(tempDist / 2, 2));
                }
            }
            Console.WriteLine("=====酒店=====");
            foreach (Hotel hotel in AllHotel)
            {
                tempDist = (float)Vector2.Distance(hotel.Location, pos);
                if (tempDist < distance)
                {
                    Console.WriteLine("{0}，距离{1}km", hotel.Name, Math.Round(tempDist / 2, 2));
                }
            }
            Console.WriteLine("=====商店=====");
            foreach (Market market in AllMarket)
            {
                tempDist = (float)Vector2.Distance(market.Location, pos);
                if (tempDist < distance)
                {
                    Console.WriteLine("{0}，距离{1}km", market.Name, Math.Round(tempDist / 2, 2));
                }
            }
        }
        public static string W_CheckStation(string stationName, float distance)
        {
            string outstring = "";
            if (AllStation.Count == 0)
            {
                outstring+="尚未初始化线路！";
                return outstring;
            }
            if (!NameDict.ContainsKey(stationName))
            {
                outstring+="没有这个站";
                return outstring;
            }
            float tempDist = 0;
            Vector2 pos = AllStation.GetItem(NameDict[stationName]).Location;
            outstring+=String.Format("  {0}站附近有：\n", stationName);
            outstring+= "=====饭店=====\n";
            foreach (Food food in AllFood)
            {
                tempDist = (float)Vector2.Distance(food.Location, pos);
                if (tempDist < distance)
                {
                    outstring+= String.Format("{0}，距离{1}km\n", food.Name, Math.Round(tempDist / 2, 2));
                }
            }
            outstring+= "=====酒店=====\n";
            foreach (Hotel hotel in AllHotel)
            {
                tempDist = (float)Vector2.Distance(hotel.Location, pos);
                if (tempDist < distance)
                {
                    outstring+= String.Format("{0}，距离{1}km\n", hotel.Name, Math.Round(tempDist / 2, 2));
                }
            }
            outstring+= "=====商店=====\n";
            foreach (Market market in AllMarket)
            {
                tempDist = (float)Vector2.Distance(market.Location, pos);
                if (tempDist < distance)
                {
                    outstring+= String.Format("{0}，距离{1}km\n", market.Name, Math.Round(tempDist / 2, 2));
                }
            }
            return outstring;
        }
        public static List<Vector2> W_CheckStationLocation(string stationName, float distance)
        {
            List<Vector2> outList=new List<Vector2>();

            float tempDist = 0;
            Vector2 pos = AllStation.GetItem(NameDict[stationName]).Location;
            foreach (Food food in AllFood)
            {
                tempDist = (float)Vector2.Distance(food.Location, pos);
                if (tempDist < distance)
                {
                    outList.Add(food.Location);
                }
            }
            foreach (Hotel hotel in AllHotel)
            {
                tempDist = (float)Vector2.Distance(hotel.Location, pos);
                if (tempDist < distance)
                {
                    outList.Add(hotel.Location);
                }
            }
            foreach (Market market in AllMarket)
            {
                tempDist = (float)Vector2.Distance(market.Location, pos);
                if (tempDist < distance)
                {
                    outList.Add(market.Location);
                }
            }
            return outList;
        }
        public static void CheckStationNearest(string stationName, float distance)
        {
            if (AllStation.Count == 0)
            {
                Console.WriteLine("尚未初始化线路！");
                return;
            }
            if (!NameDict.ContainsKey(stationName))
            {
                Console.WriteLine("没有这个站");
                return;
            }
            Vector2 pos = AllStation.GetItem(NameDict[stationName]).Location;
            if (AllFood.Count != 0)
            {
                Food nFood = AllFood[0];
                float tmpDist, minDist = 65535;

                foreach (Food food in AllFood)
                {
                    tmpDist = (float)Vector2.Distance(food.Location, pos);
                    if (tmpDist < distance && tmpDist < minDist)
                    {
                        minDist = tmpDist;
                        nFood = food;
                    }
                }
                if (minDist != 65535)
                {
                    Console.WriteLine("最近的饭店：{0}，距离为：{1}km", nFood.Name, Math.Round(minDist / 2, 2));
                }
            }
            if (AllMarket.Count != 0)
            {
                Market nMarket = AllMarket[0];
                float tmpDist, minDist = 65535;
                foreach (Market market in AllMarket)
                {
                    tmpDist = (float)Vector2.Distance(market.Location, pos);
                    if (tmpDist < distance && tmpDist < minDist)
                    {
                        minDist = tmpDist;
                        nMarket = market;
                    }
                }
                if (minDist != 65535)
                {
                    Console.WriteLine("最近的商店：{0}，距离为：{1}km", nMarket.Name, Math.Round(minDist / 2, 2));
                }
            }
            if (AllHotel.Count != 0)
            {
                Hotel nHotel = AllHotel[0];
                float tmpDist, minDist = 65535;
                foreach (Hotel hotel in AllHotel)
                {
                    tmpDist = (float)Vector2.Distance(hotel.Location, pos);
                    if (tmpDist < distance && tmpDist < minDist)
                    {
                        minDist = tmpDist;
                        nHotel = hotel;
                    }
                }
                if (minDist != 65535)
                {
                    Console.WriteLine("最近的酒店：{0}，距离为：{1}km", nHotel.Name, Math.Round(minDist / 2, 2));
                }
            }








        }
        public static string W_CheckStationNearest(string stationName, float distance)
        {
            string outstring = "";
            if (AllStation.Count == 0)
            {
                outstring+=String.Format("尚未初始化线路！");
                return outstring;
            }
            if (!NameDict.ContainsKey(stationName))
            {
                outstring+=String.Format("没有这个站");
                return outstring;
            }
            Vector2 pos = AllStation.GetItem(NameDict[stationName]).Location;
            if (AllFood.Count != 0)
            {
                Food nFood = AllFood[0];
                float tmpDist, minDist = 65535;

                foreach (Food food in AllFood)
                {
                    tmpDist = (float)Vector2.Distance(food.Location, pos);
                    if (tmpDist < distance && tmpDist < minDist)
                    {
                        minDist = tmpDist;
                        nFood = food;
                    }
                }
                if (minDist != 65535)
                {
                    outstring+=String.Format("最近的饭店：{0}，距离为：{1}km\n", nFood.Name, Math.Round(minDist / 2, 2));
                }
            }
            if (AllMarket.Count != 0)
            {
                Market nMarket = AllMarket[0];
                float tmpDist, minDist = 65535;
                foreach (Market market in AllMarket)
                {
                    tmpDist = (float)Vector2.Distance(market.Location, pos);
                    if (tmpDist < distance && tmpDist < minDist)
                    {
                        minDist = tmpDist;
                        nMarket = market;
                    }
                }
                if (minDist != 65535)
                {
                    outstring+=String.Format("最近的商店：{0}，距离为：{1}km\n", nMarket.Name, Math.Round(minDist / 2, 2));
                }
            }
            if (AllHotel.Count != 0)
            {
                Hotel nHotel = AllHotel[0];
                float tmpDist, minDist = 65535;
                foreach (Hotel hotel in AllHotel)
                {
                    tmpDist = (float)Vector2.Distance(hotel.Location, pos);
                    if (tmpDist < distance && tmpDist < minDist)
                    {
                        minDist = tmpDist;
                        nHotel = hotel;
                    }
                }
                if (minDist != 65535)
                {
                    outstring+=String.Format("最近的酒店：{0}，距离为：{1}km\n", nHotel.Name, Math.Round(minDist / 2, 2));
                }
            }
            return outstring;
        }

        public static void CheckNear()
        {
            Console.WriteLine("饭店有：");
            foreach (Food food in AllFood)
            {
                Console.WriteLine("{0}，评分：{1}，人均：{2}￥", food.Name, food.Score, food.Price);
            }
            Console.WriteLine("超市有：");
            foreach (Market market in AllMarket)
            {
                Console.WriteLine("{0}，评分：{1}，热卖品：{2}", market.Name, market.Score, market.Goods);
            }
            Console.WriteLine("酒店有：");
            foreach (Hotel hotel in AllHotel)
            {
                Console.WriteLine("{0}，{1}星级，评分：{2}，平均房价：{3}", hotel.Name, hotel.Stars, hotel.Score, hotel.Price);
            }
        }

        public static void FindBestScoreHotel()
        {
            string bestName = "";
            float tmpScore = 0;
            foreach (Hotel hotel in AllHotel)
            {
                if (hotel.Score > tmpScore)
                {
                    bestName = hotel.Name;
                    tmpScore = hotel.Score;
                }
            }
            Console.WriteLine("评价最好的酒店为" + bestName + "，评分为：" + tmpScore);
        }
        public static string W_FindBestScoreHotel()
        {
            string outstring = "";
            string bestName = "";
            float tmpScore = 0;
            foreach (Hotel hotel in AllHotel)
            {
                if (hotel.Score > tmpScore)
                {
                    bestName = hotel.Name;
                    tmpScore = hotel.Score;
                }
            }
            outstring="评价最好的酒店为" + bestName + "，评分为：" + tmpScore;
            return outstring;
        }
        /// <summary>
        /// 1=food,2=hotel,3=market
        /// </summary>
        /// <param name="nearName"></param>
        /// <param name="nearType"></param>
        public static void FindStationByNear(string nearName, int nearType)
        {
            if (AllStation.Count == 0)
            {
                Console.WriteLine("尚未初始化线路！");
                return;
            }
            float leastDist = 65535;
            string statName = "";
            switch (nearType)
            {
                case 1:
                    foreach (Food food in AllFood)
                    {
                        if (food.Name == nearName)
                        {
                            for (int i = 0; i < AllStation.Count; i++)
                            {
                                float tmpdist = (float)Vector2.Distance(AllStation.GetItem(i).Location, food.Location);
                                if (tmpdist < leastDist)
                                {
                                    leastDist = tmpdist;
                                    statName = AllStation.GetItem(i).Name;
                                }
                            }
                        }
                    }
                    Console.WriteLine("距离{0}最近的站点为{1}，距离为{2}km", nearName, statName, Math.Round(leastDist / 2, 2));
                    break;
                case 2:
                    foreach (Hotel hotel in AllHotel)
                    {
                        if (hotel.Name == nearName)
                        {
                            for (int i = 0; i < AllStation.Count; i++)
                            {
                                float tmpdist = (float)Vector2.Distance(AllStation.GetItem(i).Location, hotel.Location);
                                if (tmpdist < leastDist)
                                {
                                    leastDist = tmpdist;
                                    statName = AllStation.GetItem(i).Name;
                                }
                            }
                        }
                    }
                    Console.WriteLine("距离{0}最近的站点为{1}，距离为{2}km", nearName, statName, Math.Round(leastDist / 2, 2));

                    break;
                case 3:
                    foreach (Market market in AllMarket)
                    {
                        if (market.Name == nearName)
                        {
                            for (int i = 0; i < AllStation.Count; i++)
                            {
                                float tmpdist = (float)Vector2.Distance(AllStation.GetItem(i).Location, market.Location);
                                if (tmpdist < leastDist)
                                {
                                    leastDist = tmpdist;
                                    statName = AllStation.GetItem(i).Name;
                                }
                            }
                        }
                    }
                    Console.WriteLine("距离{0}最近的站点为{1}，距离为{2}km", nearName, statName, Math.Round(leastDist / 2, 2));
                    break;
                default:
                    Console.WriteLine("输入类型错误！");
                    break;
            }
        }
        public static string W_FindStationByNear(string nearName, int nearType)
        {
            string outstring = "";
            if (AllStation.Count == 0)
            {
                outstring+=String.Format("尚未初始化线路！");
                return outstring;
            }
            float leastDist = 65535;
            string statName = "";
            switch (nearType)
            {
                case 1:
                    foreach (Food food in AllFood)
                    {
                        if (food.Name == nearName)
                        {
                            for (int i = 0; i < AllStation.Count; i++)
                            {
                                float tmpdist = (float)Vector2.Distance(AllStation.GetItem(i).Location, food.Location);
                                if (tmpdist < leastDist)
                                {
                                    leastDist = tmpdist;
                                    statName = AllStation.GetItem(i).Name;
                                }
                            }
                        }
                    }
                    if (leastDist==65535)
                    {
                        break;
                    }
                    outstring=String.Format("距离{0}最近的站点为{1}，距离为{2}km", nearName, statName, Math.Round(leastDist / 2, 2));
                    break;
                case 2:
                    foreach (Hotel hotel in AllHotel)
                    {
                        if (hotel.Name == nearName)
                        {
                            for (int i = 0; i < AllStation.Count; i++)
                            {
                                float tmpdist = (float)Vector2.Distance(AllStation.GetItem(i).Location, hotel.Location);
                                if (tmpdist < leastDist)
                                {
                                    leastDist = tmpdist;
                                    statName = AllStation.GetItem(i).Name;
                                }
                            }
                        }
                    }
                    if (leastDist == 65535)
                    {
                        break;
                    }
                    outstring =String.Format("距离{0}最近的站点为{1}，距离为{2}km", nearName, statName, Math.Round(leastDist / 2, 2));

                    break;
                case 3:
                    foreach (Market market in AllMarket)
                    {
                        if (market.Name == nearName)
                        {
                            for (int i = 0; i < AllStation.Count; i++)
                            {
                                float tmpdist = (float)Vector2.Distance(AllStation.GetItem(i).Location, market.Location);
                                if (tmpdist < leastDist)
                                {
                                    leastDist = tmpdist;
                                    statName = AllStation.GetItem(i).Name;
                                }
                            }
                        }
                    }
                    if (leastDist == 65535)
                    {
                        break;
                    }
                    outstring =String.Format("距离{0}最近的站点为{1}，距离为{2}km", nearName, statName, Math.Round(leastDist / 2, 2));
                    break;
                default:
                    outstring+=String.Format("输入类型错误！");
                    break;
            }
            return outstring;
        }

        private static Station FindS(int stIndex, string line, Station target)
        {
            for (int i = 1; i < LineDict[line].Count - 1; i++)
            {
                int searchIndex = stIndex + i;
                if (searchIndex >= 0 && searchIndex <= LineDict[line].Count)
                {
                    if (LineDict[line][stIndex + i] == target)
                    {
                        return LineDict[line][stIndex + i];
                    }
                }
                searchIndex = stIndex - i;
                if (searchIndex >= 0 && searchIndex <= LineDict[line].Count)
                {
                    if (LineDict[line][stIndex - i] == target)
                    {
                        return LineDict[line][stIndex - i];
                    }
                }
            }
            return LineDict[line][stIndex];
        }
    }
}
